const BaseController = require("./base");

class Controller extends BaseController {
	constructor(...args) {
		super(...args);
		this.table = "role";
		this.createRule = {
			name: "string"
		};
		this.updateRule = this.createRule;
	}

	async getallRole() {
		const { ctx } = this;
		const res = await this.app.mysql.select("role");

		ctx.body = res;
	}

	async getPerm() {
		const { ctx, service } = this;
		const { id } = ctx.params;
		const res = await service.role.getPerm(id);
		const perms = [];
		res.forEach((item) => {
			perms.push(item.id);
		});

		ctx.body = perms;
	}

	async setPerm() {
		const { ctx, service } = this;
		const body = ctx.request.body;
		await service.role.setPerm(body);

		this.success("授权成功");
	}
}

module.exports = Controller;
